package com.oazon.system;

import java.io.File;

import android.app.Service;
import android.content.Intent;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.oazon.moco.link.core.LinkEventController;
import com.oazon.moco.link.core.LinkSDK;
import com.oazon.moco.link.core.source.LinkEventSource;
import com.oazon.moco.link.ui.SystemStateEventListener;
import com.oazon.system.action.NormalAction;
import com.oazon.system.action.ScreenProtectAction;
import com.oazon.system.action.ShutDownAction;
import com.oazon.system.action.StandByAcion;

public class SystemService extends Service {

	private static final String TAG = "SystemService";

	@Override
	public void onCreate() {
		SysActionChannal.getInstance();
		LinkSDK.getInstance().registLinkListener(systemStateEventListener);
		super.onCreate();
	}

	@Override
	public IBinder onBind(Intent arg0) {

		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		LinkSDK.getInstance().isNormal(new LinkEventSource());
		return super.onStartCommand(intent, flags, startId);
	}

	SystemStateEventListener systemStateEventListener = new SystemStateEventListener(
			LinkEventController.systemEventSource) {

		@Override
		protected void waite() {
			SysActionChannal.getInstance().doAction(new StandByAcion());
		}

		@Override
		protected void screenProtect() {
			SysActionChannal.getInstance().doAction(new ScreenProtectAction());
		}

		@Override
		protected void opendown() {
			SysActionChannal.getInstance().doAction(new NormalAction());
		}

		@Override
		protected void shutdown() {
			SysActionChannal.getInstance().doAction(new ShutDownAction());
		}

		@Override
		protected void hasAction() {
			SysActionChannal.getInstance().doAction(new NormalAction());
		}

		@Override
		protected void normal() {
			Log.d(TAG, "normalnormalnormalnormal");
			clean();
			/*VolumeInstance volumeInstance = new VolumeInstance();
			
			List<Volume> volumes = new ArrayList<Volume>();
			volumes.add(new Volume(VolumeChannel.MASTER,Settings.getInstance(getApplicationContext()).getInt(SettingType.VOLUME.value())));
			volumeInstance.setVolume(volumes);
			
			LinkSDK.getInstance().setVolume(LinkEventController.uiSource, volumeInstance);*/
			SysActionChannal.getInstance().doAction(new NormalAction());
		}

	};

	@Override
	public void onDestroy() {
		super.onDestroy();
	};

	public void clean() {
		try {
			String path = Environment.getExternalStorageDirectory()
					.getAbsolutePath() + "/" + "download";
			
			File f = new File(path);

			File[] fl = f.listFiles();
			for (int i = 0; i < fl.length; i++) {
				if (fl[i].toString().endsWith(".apk")) {
					if (fl[i].delete()) {
						Toast.makeText(getApplicationContext(), "成功",
								Toast.LENGTH_LONG).show();
					} else {
						Toast.makeText(getApplicationContext(), "失败",
								Toast.LENGTH_LONG).show();
					}
				}
			}
		} catch (Exception e) {
			Log.d(TAG, "clean check error");
		}
	}
}
